package com.vasthu.gui;



import com.vasthu.gui.VstPluginAPI;
import javax.swing.*;
import java.awt.event.*;
import java.lang.reflect.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class VstPluginManager {

	public static final String PLUGIN = "plugin";
	public static final String MENU_NAME = "menu-name";
	public static final String CLASS_NAME = "class-name";
	public static final String TOOLTIP = "tooltip";
	public static final String ICON_FILE = "icon";

	private static final VstPluginManager inst = new VstPluginManager();
	private JMenuBar menubar;
	private JMenu plugins;
	
	private VstPluginManager() {
	}
	
	public static VstPluginManager get() {
		return inst;
	}
	
	public void setMenuBar(JMenuBar menubar) {
		this.menubar = menubar;
		plugins = new JMenu("Extra");
		menubar.add(plugins);
	}
	
	public void processXml() {
		List<VstPlugin> plugList = readXml();
		
		for (VstPlugin plug : plugList) {
			
			String iconFile = "./plugins/" + plug.getIconFile();
			ImageIcon icon = new ImageIcon(getClass().getResource(iconFile));
			
			JMenuItem action = new JMenuItem(plug.getMenuName(), icon);
			action.setToolTipText(plug.getTooltip());
			
			final String className = plug.getClassName();
			
			action.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent event) 
				{
					try {
						Class<?> c = Class.forName(className);
					
						VstPluginAPI api = (VstPluginAPI)c.newInstance();
						api.PAPI_start();
					} catch (Exception ex) {
					
					}
				}
			});
			
			plugins.add(action);
		}
	}
	
	private List<VstPlugin> readXml() {
		List<VstPlugin> plugList = new ArrayList<VstPlugin>();
		
		try {
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();

			InputStream in = new FileInputStream("./plugins/plugins.xml");
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);

			VstPlugin plug = null;

			while (eventReader.hasNext()) {
				XMLEvent event = eventReader.nextEvent();
				
				// read plugin
				if (event.isStartElement()) {
					StartElement startElement = event.asStartElement();

					if (startElement.getName().getLocalPart() == (PLUGIN)) {
						plug = new VstPlugin();
					}
				}

				// read menu name
				if (event.isStartElement()) {
					if (event.asStartElement().getName().getLocalPart().equals(MENU_NAME)) {
						event = eventReader.nextEvent();
						plug.setMenuName(event.asCharacters().getData());
						continue;
					}
				}
					
				// read class name
				if (event.isStartElement()) {
					if (event.asStartElement().getName().getLocalPart().equals(CLASS_NAME)) {
						event = eventReader.nextEvent();
						plug.setClassName(event.asCharacters().getData());
						continue;
					}
				}
				
				// read tooltip
				if (event.isStartElement()) {
					if (event.asStartElement().getName().getLocalPart().equals(TOOLTIP)) {
						event = eventReader.nextEvent();
						plug.setTooltip(event.asCharacters().getData());
						continue;
					}
				}
				
				// read icon
				if (event.isStartElement()) {
					if (event.asStartElement().getName().getLocalPart().equals(ICON_FILE)) {
						event = eventReader.nextEvent();
						plug.setIconFile(event.asCharacters().getData());
						continue;
					}
				}

				// end plugin
				if (event.isEndElement()) {
					EndElement endElement = event.asEndElement();
					if (endElement.getName().getLocalPart() == (PLUGIN)) {
						plugList.add(plug);
					}
				}
			}
		} catch (Exception ex) {
		}
		
		return plugList;
	}

}